package com.dehui.traders.ui.strategy;

import android.content.Context;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.bean.BaseListResponse;
import com.dehui.traders.bean.StrategyInfo;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.utils.UserHelp;
import com.dehui.traders.widgets.BaseViewHolder;
import com.dehui.traders.widgets.CommonAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by hqq on 18.3.30.
 */

public class SmartChooseStocksViewModel extends BaseViewModel{

    public List<StrategyInfo> strateList = new ArrayList<>();

    public CommonAdapter trendListAdapter = new CommonAdapter<StrategyInfo>(context,strateList,
            R.layout.list_item_strate){

        @Override
        public void convert(BaseViewHolder helper, StrategyInfo item, int position) {
            TextView tvTitle = helper.getView(R.id.tv_title);
            TextView tvContent = helper.getView(R.id.tv_content);
            CardView cardView = helper.getView(R.id.cardview);
            tvTitle.setText(item.getNAME());
            tvContent.setText(item.getDESCRIPTION());

            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<StrategyInfo.StrategyStock> stocks = item.getStrategyStock();
                    Bundle bundle = new Bundle();
                    if (stocks != null )
                        bundle.putSerializable(StrateDetailFragment.IntentKey_Strate, (Serializable) stocks);
                    bundle.putString(StrateDetailFragment.IntentKey_Name,item.getNAME());
                    startContainerActivity(StrateDetailFragment.class.getCanonicalName(),bundle);

                }
            });
        }
    };

    public ObservableField<CommonAdapter> strateAdapterObservable = new ObservableField<>(trendListAdapter);

    public SmartChooseStocksViewModel(Context context) {
        super(context);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initStrategy();
    }

    private void initStrategy() {
        Call<BaseListResponse<StrategyInfo>> call = NetImpl.queryStrategy(UserHelp.getUserId());
        call.enqueue(new CallListener<BaseListResponse<StrategyInfo>>() {
            @Override
            public void _onResponse(Response<BaseListResponse<StrategyInfo>> response) {
                BaseListResponse<StrategyInfo> body = response.body();
                if (body.status == 200){
                    List<StrategyInfo> result = body.result;
                    strateList.clear();
                    strateList.addAll(result);
                    trendListAdapter.notifyDataSetChanged();
                }

            }

            @Override
            public void _onFailure() {

            }
        });
    }

}
